<?
function getConferences() {
	global $returnConferences, $dbname, $db, $db_selected;
	$returnString = '<h2>Conferences</h2>';
	$returnString .= '<div id="addConference" style="float: left; width: 50%;">';
	$returnString .= '<a href="#" onClick="addConference(); return false;"><b>Add Conference</b></a>';
	$returnString .= '</div>';
	$returnString .= '<div id="ajaxAnimConference" style="float: left; width: 50%;"></div>';
	$returnString .= '<div style="clear: left;"></div>';
	
	$sql = "SELECT * from transConferences ORDER BY DateStart DESC";
	$result = mysql_db_query($dbname,$sql);
	$rows = mysql_num_rows($result);
	if ($rows>0) {
		$returnString .= '<table class="dataTable">';
		$returnString .= '
						<tr>
							<th style="width: 10%;"></th>
							<th style="width: 60%;">Conference Title &amp; Description</th>
							<th style="width: 23%;">Date(s)</th>
							<th style="width: 7%; text-align: center;">On Site</th>
						</tr>
		';
		$i=1;
		while ($value = mysql_fetch_assoc($result)) {
			if (($i % 2) > 0) { $class = ''; }
			else { $class = "altRow"; }
			
			$cID         = $value[CID];	
			$dateStart   = $value[DateStart];
			$dateEnd     = $value[DateEnd];
			$title       = stripslashes($value[Title]);
			$description = stripslashes($value[ConfDescription]);
			$url         = $value[ConfLink];
			$active      = $value[Active];
				
			$returnString .= '<tr class="'.$class.'">';
			$returnString .= '<td style="text-align: center;">';
								
			$returnString .= '<a href="#" onClick="editConference('.$cID.'); return false;" title="Edit Conference"><img src="images/button_edit.gif" style="border: none;" alt="Edit Conference"></a>';
			$returnString .= '<a href="#" onClick="confirmDeleteItem('.$cID.',\'conference\'); return false;" title="Delete Conference"><img src="images/button_delete.gif" style="border: none;" alt="Delete Conference"></a>';					
								
			$returnString .= '</td>';
			$returnString .= '<td>
								<b>'.$title.'</b><br />
								'.$description.'<br />';
			if (!empty($url)) {
				$returnString .= '<a href="'.$url.'" target="_blank">'.$url.'</a>';
			}				
			$returnString .= '</td><td style="vertical-align: top;">';
			$returnString .= mysql_to_date2($dateStart);
			if ($dateEnd != '0000-00-00') {
				$returnString .= ' To '.mysql_to_date2($dateEnd);
			}					
			
			if ($active == 1) { $activeIcon = 'iconYes.gif'; $activeAlt = 'Remove from site'; }
			else { $activeIcon = 'iconNo.gif'; $activeAlt = 'Display on site'; }
			
			$returnString .= '</td><td style="text-align: center; vertical-align: top;" id="conferenceToggleActive_'.$cID.'">';
			
			$returnString .= '<a href="#" onClick="toggleActive('.$cID.','.$active.',\'conference\'); return false;" title="'.$activeAlt.'"><img src="images/'.$activeIcon.'" style="border: none;" alt="'.$activeAlt.'"></a>';
			
			$returnString .= '</td></tr>';
				
			$i++;
		}
		$returnString .= '</table>';
	} else {
		$returnString .= 'There are no conferences entered at this time.';
	}
	
	$returnConferences = $returnString.'|conference';
	return $returnConferences;	
}

function addConference($dateStart,$dateEnd=null,$title,$description=null,$url=null) {
	global $returnConferences, $dbname, $db, $db_selected;
	
	$sql = "INSERT into transConferences (
			DateStart,
			DateEnd,
			Title,
			ConfDescription,
			ConfLink,
			Active,
			DateAdded
			) values (
			'$dateStart',
			'$dateEnd',
			'$title',
			'$description',
			'$url',
			'1',
			'".date('Y-m-d')."'
			)";
	$result = mysql_db_query($dbname,$sql);
	getConferences();
	
	$returnConferences .= '|conference';
	return $returnConferences;
}

function editConference($cID) {
	global $returnEditString, $dbname, $db, $db_selected;
	
	$sql = "SELECT * from transConferences WHERE CID = '$cID'";
	$result = mysql_db_query($dbname,$sql);
	$row = mysql_fetch_assoc($result);
	
	$dateStart   = $row[DateStart];
	$dateEnd     = $row[DateEnd];
	$title       = DBToField($row[Title]);
	$description = DBToField($row[ConfDescription]);
	$url         = $row[ConfLink];
	
	$returnEditString = '<div id="addEditConference2">
						<h3>Edit Conference</h3>
						<table style="border: none;" class="inputForm"><tr>
						<td style="width: 40%; vertical-align: top;">Conference Date(s)</td>
						<td style="width: 60%;">
						
						<input type="text" name="dateStart" id="dateStart" style="width: 80px;" onclick="this.blur(); return showCalendar(\'dateStart\', \'y-mm-dd\');" value="'.$dateStart.'">
						&nbsp;<b>To</b>&nbsp;
						<input type="text" name="dateEnd" id="dateEnd" style="width: 80px;" onclick="this.blur(); return showCalendar(\'dateEnd\', \'y-mm-dd\');" value="';
	if ($dateEnd != '0000-00-00') {
		$returnEditString .= $dateEnd;
	}					
	$returnEditString .= '">
	
						</td></tr><tr><td style="vertical-align: top;">Conference Title</td><td>
						
						<input type="text" id="title" value="'.$title.'" style="width: 300px;">
						
						</td></tr><tr><td style="vertical-align: top;">Conference Description</td><td>
						
						<textarea id="description" style="width: 300px; height: 80px;">'.$description.'</textarea>
						
						</td></tr><tr><td style="vertical-align: top;">Conference Website</td><td>
						
						<input type="text" id="url" style="width: 300px;" value="'.$url.'" onFocus="this.value=\'\'">
						
						</td></tr><tr><td style="vertical-align: top;"></td><td><input type="submit" onClick="editConferenceConfirm('.$cID.'); return false;" value="Edit Conference">
						<input type="button" value="Close" onClick="closeWindow(\'addEditConference\'); return false;">
						</td></tr></table></div>';
	
	return $returnEditString;	
}

function editConferenceConfirm($dateStart,$dateEnd=null,$title,$description=null,$url=null,$cID) {
	global $returnConferences, $dbname, $db, $db_selected;
	
	$sql = "UPDATE transConferences SET
			DateStart = '$dateStart',
			DateEnd   = '$dateEnd',
			Title     = '$title',
			ConfDescription = '$description',
			ConfLink  = '$url'
			WHERE CID = '$cID'";
	$result = mysql_db_query($dbname,$sql);
	getConferences();
	
	$returnConferences .= '|conference';
	return $returnConferences;
}

function getNewsletters() {
	global $returnNewsletters, $dbname, $db, $db_selected;
	$returnString = '<h2>Newsletters</h2>';
	$returnString .= '<div id="addNewsletter" style="float: left; width: 50%;">';
	$returnString .= '<a href="#" onClick="addNewsletter(); return false;"><b>Add Newsletter</b></a>';
	$returnString .= '</div>';
	$returnString .= '<div id="ajaxAnimNewsletter" style="float: left; width: 50%;"></div>';
	$returnString .= '<div style="clear: left;"></div>';
	
	$sql = "SELECT * from transNewsletters ORDER BY DateAdded DESC";
	$result = mysql_db_query($dbname,$sql);
	$rows = mysql_num_rows($result);
	if ($rows>0) {
		$returnString .= '<table class="dataTable">';
		$returnString .= '
						<tr>
							<th style="width: 10%;"></th>
							<th style="width: 60%;">Title &amp; Description</th>
							<th style="width: 23%;">Web Link</th>
							<th style="width: 7%; text-align: center;">On Site</th>
						</tr>
		';
		$i=1;
		while ($value = mysql_fetch_assoc($result)) {
			if (($i % 2) > 0) { $class = ''; }
			else { $class = "altRow"; }
			
			$nID         = $value[NID];	
			$title       = stripslashes($value[Title]);
			$description = stripslashes($value[NLDescription]);
			$url         = $value[NLLink];
			$active      = $value[Active];
				
			$returnString .= '<tr class="'.$class.'">';
			$returnString .= '<td style="text-align: center;">';
								
			$returnString .= '<a href="#" onClick="editNewsletter('.$nID.'); return false;" title="Edit Newsletter"><img src="images/button_edit.gif" style="border: none;" alt="Edit Newsletter"></a>';
			$returnString .= '<a href="#" onClick="confirmDeleteItem('.$nID.',\'newsletter\'); return false;" title="Delete Newsletter"><img src="images/button_delete.gif" style="border: none;" alt="Delete Newsletter"></a>';					
								
			$returnString .= '</td>';
			$returnString .= '<td style="vertical-align: top;"><b>'.$title.'</b><br />'.$description.'</td>';
							
			$returnString .= '</td><td style="vertical-align: top;">';
			if (!empty($url)) {
				if (strlen($url)>50) {
					$displayURL = substr($url,0,50).'...';
				} else {
					$displayURL = $url;
				}
				$returnString .= '<a href="'.$url.'" target="_blank" title="Visit '.$url.'">'.$displayURL.'</a>';
			}				
			if ($active == 1) { $activeIcon = 'iconYes.gif'; $activeAlt = 'Remove from site'; }
			else { $activeIcon = 'iconNo.gif'; $activeAlt = 'Display on site'; }
			
			$returnString .= '</td><td style="text-align: center; vertical-align: top;" id="newsletterToggleActive_'.$nID.'">';
			
			$returnString .= '<a href="#" onClick="toggleActive('.$nID.','.$active.',\'newsletter\'); return false;" title="'.$activeAlt.'"><img src="images/'.$activeIcon.'" style="border: none;" alt="'.$activeAlt.'"></a>';
			
			$returnString .= '</td></tr>';
				
			$i++;
		}
		$returnString .= '</table>';
	} else {
		$returnString .= 'There are no newsletters entered at this time.';
	}
	
	$returnNewsletters = $returnString.'|newsletter';
	return $returnNewsletters;	
}

function addNewsletter($title,$description=null,$url=null) {
	global $returnNewsletters, $dbname, $db, $db_selected;
	
	$sql = "INSERT into transNewsletters (
			Title,
			NLDescription,
			NLLink,
			DateAdded,
			Active
			) values (
			'$title',
			'$description',
			'$url',
			'".date('Y-m-d')."',
			'1'
			)";
	$result = mysql_db_query($dbname,$sql);
	getNewsletters();
	
	return $returnNewsletters;
}

function editNewsletter($nID) {
	global $returnEditString, $dbname, $db, $db_selected;
	
	$sql = "SELECT * from transNewsletters WHERE NID = '$nID'";
	$result = mysql_db_query($dbname,$sql);
	$row = mysql_fetch_assoc($result);
	
	$title       = DBToField($row[Title]);
	$description = DBToField($row[NLDescription]);
	$url         = $row[NLLink];
	
	$returnEditString = '<div id="addEditNewsletter2">
						<h3>Edit Newsletter</h3>
						<table style="border: none;" class="inputForm"><tr>
						<td style="width: 40%; vertical-align: top;">Conference Title</td>
						<td style="width: 60%;">						
						
						<input type="text" id="title" value="'.$title.'" style="width: 300px;">
						
						</td></tr><tr><td style="vertical-align: top;">Newsletter Description</td><td>
						
						<textarea id="description" style="width: 300px; height: 80px;">'.$description.'</textarea>
						
						</td></tr><tr><td style="vertical-align: top;">Newsletter Website</td><td>
						
						<input type="text" id="url" style="width: 300px;" value="'.$url.'" onFocus="this.value=\'\'">
						
						</td></tr><tr><td style="vertical-align: top;"></td><td><input type="submit" onClick="editNewsletterConfirm('.$nID.'); return false;" value="Edit Newsletter">
						<input type="button" value="Close" onClick="closeWindow(\'addEditNewsletter\'); return false;">
						</td></tr></table></div>';
	
	return $returnEditString;	
}

function editNewsletterConfirm($title,$description=null,$url=null,$nID) {
	global $returnNewsletters, $dbname, $db, $db_selected;
	
	$sql = "UPDATE transNewsletters SET
			Title     = '$title',
			NLDescription = '$description',
			NLLink  = '$url'
			WHERE NID = '$nID'";
	$result = mysql_db_query($dbname,$sql);
	getNewsletters();
	
	$returnNewsletters .= '|newsletter';
	
	return $returnNewsletters;
}

function getServiceProviders() {

}

function deleteItem($itemID,$itemType) {
	global $returnString, $returnConferences, $returnNewsletters, $returnServiceProviders, $dbname, $db, $db_selected;

	// Which type of resource are we dealing with?
	if ($itemType == 'conference') {
		$sql = "DELETE from transConferences WHERE CID = '$itemID'";
		$result = mysql_db_query($dbname,$sql);
		
		getConferences();
		$returnString = $returnConferences;
	} elseif ($itemType == 'newsletter') {
		$sql = "DELETE from transNewsletters WHERE NID = '$itemID'";
		$result = mysql_db_query($dbname,$sql);
		
		getNewsletters();
		$returnString = $returnNewsletters;
	
	} elseif ($itemType == 'service provider') {
	
	}
	//$returnString = $sql;
	return $returnString;
}

function toggleActive($itemID,$activeState,$itemType) {
	global $toggleString, $dbname, $db, $db_selected;

	if ($activeState == 0) { $newState = 1; }
	else { $newState = 0; }
	
	if ($itemType == 'conference') { $dbTable = 'transConferences'; $tableID = 'CID'; }
	if ($itemType == 'newsletter') { $dbTable = 'transNewsletters'; $tableID = 'NID'; }
	
	$sql = "UPDATE $dbTable SET Active = '$newState' WHERE $tableID = '$itemID'";
	$result = mysql_db_query($dbname,$sql);
	
	$toggleString = $newState.'|'.$itemID.'|'.$itemType;
	return $toggleString;
}

?>